package com.bignerdranch.android.expresshelper.Fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.volley.Response;
import com.bignerdranch.android.expresshelper.Adapter.PhoneListAdapter;
import com.bignerdranch.android.expresshelper.R;
import com.bignerdranch.android.expresshelper.Utils.CheckNet;
import com.bignerdranch.android.expresshelper.Utils.RequestUtil;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.PipedReader;

/**
 * Created by Administrator on 2016/6/24.
 */
public class PhoneFragment extends Fragment {
    private TextView mTopText;
    private PullToRefreshListView mPhoneList;
    private String url = "http://route.showapi.com/64-20?showapi_appid=20076&showapi_sign=b697ebeae51b4525b7ea856bade2d325";
    private PhoneListAdapter mAdapter;
    private LinearLayout mBar;
    private LinearLayout mWifi;
    private int count = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_phone,container,false);

        mTopText = (TextView)view.findViewById(R.id.top_text);
        mTopText.setText(R.string.phone);

        mPhoneList = (PullToRefreshListView)view.findViewById(R.id.pull_to_refresh_view);
        mBar = (LinearLayout) view.findViewById(R.id.progress);
        mWifi = (LinearLayout)view.findViewById(R.id.wifi);

        mPhoneList.setMode(PullToRefreshBase.Mode.PULL_FROM_END);

        if(CheckNet.getNet(getContext())){

            RequestUtil.executeJSONObject(getActivity(), url, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject o) {
                    try {

                        mPhoneList.setVisibility(View.VISIBLE);
                        mBar.setVisibility(View.INVISIBLE);
                        mWifi.setVisibility(View.INVISIBLE);
                        mAdapter = new PhoneListAdapter(getActivity(),o.getJSONObject("showapi_res_body")
                                .getJSONArray("expressList"));
                        mPhoneList.setAdapter(mAdapter);

                    } catch (JSONException e) {
                        e.printStackTrace();

                    }
                }
            });
        }else {
            mPhoneList.setVisibility(View.INVISIBLE);
            mBar.setVisibility(View.INVISIBLE);
            mWifi.setVisibility(View.VISIBLE);
        }


        mPhoneList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String num = mAdapter.getNum(position);
                Log.e("----num>>>",num+"");//pulltorefresh的position返回值从1开始
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
                        + num));
                startActivity(intent);
            }
        });
        mPhoneList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                count++;
                RequestUtil.executeJSONObject(getActivity(), url + "&page=" + count, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject o) {
                        try {
                            mAdapter.addDate(o.getJSONObject("showapi_res_body").getJSONArray("expressList"));
                            mAdapter.notifyDataSetChanged();
//                            mPhoneList.setAdapter(mAdapter);
                            mPhoneList.onRefreshComplete();

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

            }
        });

        return view;
    }
}
